﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using YaAppoint.Appointments.AutoAppoint;
using YaAppoint.Appointments.HisAppoint;
using YaAppoint.CustomSettings;
using YaAppoint.ExamApplications;
using YaAppoint.Response;

namespace YaAppoint.Web.Controllers
{
    public class HisAppointController(IAutoAppointAppService autoAppointAppService,
                                      IExamApplyAppService examApplyAppService,
                                      IHisAppointAppService hisAppointAppService,
                                      ICustomSettingAppService customSettingAppService,
                                      IConfiguration configuration ) : AbpController
    {
        private readonly IAutoAppointAppService _autoAppointAppService = autoAppointAppService;
        private readonly ICustomSettingAppService _appointSetting = customSettingAppService;
        private readonly IConfiguration _configuration= configuration;
        private readonly IHisAppointAppService _hisAppointAppService = hisAppointAppService;
        private readonly IExamApplyAppService _examApplyAppService= examApplyAppService;

        /// <summary>
        /// 同步申请单列表下所有申请项目
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/sync-apply-items")]
        public async Task<IActionResult> SyncApplyItems(string applyItems)
        {
            return new JsonResult(await _examApplyAppService.SyncApplyItemsAsync(applyItems));
        }
        

        /// <summary>
        /// 获取未预约申请项目
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/get-noappoint-apply-items")]
        public async Task<IActionResult> GetNoAppointApplyItems(string applyNumbers)
        {
            return new JsonResult(await _hisAppointAppService.GetNoAppointApplyItems(applyNumbers));
        }

        /// <summary>
        /// 获取已预约申请项目
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/get-appointed-apply-items")]
        public async Task<IActionResult> GetAppointedApplyItems(string applyNumbers)
        {
            return new JsonResult(await _hisAppointAppService.GetAppointedApplyItems(applyNumbers));
        }


        /// <summary>
        /// 获取医院名称
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/hospital-name")]
        public IActionResult GetHospitalName()
        {
            return new JsonResult(new BackJson { 
                code =0,
                data =new {
                    hospitalName= _configuration!["InstitutionName"]
                } 
            }); 
        }

        /// <summary>
        /// 获取全局设置
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/global-appoint-setting")]
        public async Task<IActionResult> GlobalAppointSetting()
        {
            var res = new BackJson()
            {
                code = 0,
                data = await _appointSetting.GetGlobalSetting()
            };
            return new JsonResult(res);
        }

        /// <summary>
        /// 获取患者信息
        /// </summary>
        [AllowAnonymous] 
        [HttpGet("/api/his-appoint/get-patinet-info")]
        public async Task<IActionResult> GetPatientInfo(string applyNumbers)
        {
            return new JsonResult(await _hisAppointAppService.GetPatientInfo(applyNumbers));
        }

        /// <summary>
        /// 一次性加载页面初始化信息(避免多次请求造成性能浪费)
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/get-init-info")]
        public async Task<IActionResult> GetInitInfo(string applyNumbers)
        {
            return new JsonResult(await _hisAppointAppService.GetInitInfo(applyNumbers));
        }

        /// <summary>
        /// 获取推荐时间段
        /// </summary>
        [AllowAnonymous]
        [HttpPost("/api/his-appoint/get-suggestion")]
        public async Task<IActionResult> GetIntervals([FromBody]Condition condition)
        {
            return new JsonResult(await _hisAppointAppService.GetSuggestInterval(condition.ApplyItems, DateTime.Parse(condition.Date).Date));
        }


        /// <summary>
        /// 提交预约
        /// </summary>
        [AllowAnonymous]
        [HttpPost("/api/his-appoint/submit-appoint")]
        public async Task<IActionResult> AppointSubmit([FromBody]HisAppointDto<ApplyItemDto> dto)
        {
            return new JsonResult(await _hisAppointAppService.AppointSubmit(dto.ApplyItems,dto.Confirmed));
        }


        /// <summary>
        /// 修改预约
        /// </summary>
        [AllowAnonymous]
        [HttpPost("/api/his-appoint/edti-submit")]
        public async Task<IActionResult> EditSubmit([FromBody]HisAppointDto<ApplyItemDto> dto)
        {
            return new JsonResult(await _hisAppointAppService.EditAppoints(dto.ApplyItems, dto.Confirmed));
        }

        /// <summary>
        /// 确认预约方案
        /// </summary>
        [AllowAnonymous]
        [HttpGet("/api/his-appoint/confirm-solution")]
        public async Task<IActionResult> ConfirmSolution([FromQuery] string applyNumbers)
        {
            return new JsonResult(await _hisAppointAppService.ConfirmSolution(applyNumbers));
        }

        /// <summary>
        /// 取消预约
        /// </summary>
        [AllowAnonymous]
        [HttpPost("/api/his-appoint/cancel")]
        public async Task<IActionResult> Cancel([FromBody] HisAppointDto<ApplyItemDto2> dto)
        {
            return new JsonResult(await _hisAppointAppService.Cancel(dto.ApplyItems.Select(a=>a.Id).ToList(),dto.Confirmed));
        }
        
    }

    public class Condition
    { 
        public List<Guid> ApplyItems { get; set; }

        public string Date { get; set; }
    }
}
